package scct.hanyuesheng.utils;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtils {
//    数据库连接字符串
    private  static  String Driver="com.mysql.cj.jdbc.Driver";
    private static String dbUrl =
            "jdbc:mysql://localhost:3306/storedb?characterEncoding=UTF-8&useSSL=false";


    private static  String name="root";
    private static  String pass="123456";

//    获取数据库连接方法
    public static Connection getConnection()  {
        Connection conn;
        try{
            Class.forName(Driver);//加载驱动
            conn= DriverManager.getConnection(dbUrl,name,pass);
        }catch (ClassNotFoundException | SQLException e){
            throw new RuntimeException(e);
        }
        return conn;
    }
    public static void closeConnection(Connection conn){
        if(conn!=null){
            try{
                conn.close();
            }catch (SQLException e){
                throw new RuntimeException(e);
            }
        }
    }
//    关闭数据库
}
